﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SellerAD
    {
        public int Add(KeRui.Model.SellerAD model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SellerID", SqlDbType.Int, 4), new SqlParameter("@AdID", SqlDbType.Int, 4), new SqlParameter("@AdText", SqlDbType.NVarChar, 0xff), new SqlParameter("@AdUrl", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@StartDate", SqlDbType.DateTime), new SqlParameter("@EndDate", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@AdName", SqlDbType.NVarChar, 50) };
            parameters[0].Value = model.SellerID;
            parameters[1].Value = model.AdID;
            parameters[2].Value = model.AdText;
            parameters[3].Value = model.AdUrl;
            parameters[4].Value = model.StartDate;
            parameters[5].Value = model.EndDate;
            parameters[6].Value = model.CreateIP;
            parameters[7].Value = model.State;
            parameters[8].Value = model.AdName;
            return DbHelperSQL.RunProcedure("SellerAD_ADD", parameters, out num);
        }

        public int CheckTime(int AdID, DateTime StartDate, DateTime EndDate, int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdID", SqlDbType.Int), new SqlParameter("@StartDate", SqlDbType.DateTime), new SqlParameter("@EndDate", SqlDbType.DateTime), new SqlParameter("@ID", SqlDbType.Int) };
            parameters[0].Value = AdID;
            parameters[1].Value = StartDate;
            parameters[2].Value = EndDate;
            parameters[3].Value = ID;
            return DbHelperSQL.RunProcedure("SellerAD_CheckTime", parameters, out num);
        }

        public int Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            return DbHelperSQL.RunProcedure("SellerAD_Delete", parameters, out num);
        }

        public KeRui.Model.SellerAD GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.SellerAD rad = new KeRui.Model.SellerAD();
            DataSet set = DbHelperSQL.RunProcedure("SellerAD_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    rad.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["SellerID"].ToString() != "")
                {
                    rad.SellerID = int.Parse(set.Tables[0].Rows[0]["SellerID"].ToString());
                }
                rad.SellerName = set.Tables[0].Rows[0]["SellerName"].ToString();
                if (set.Tables[0].Rows[0]["AdID"].ToString() != "")
                {
                    rad.AdID = int.Parse(set.Tables[0].Rows[0]["AdID"].ToString());
                }
                rad.AdName = set.Tables[0].Rows[0]["AdName"].ToString();
                if (set.Tables[0].Rows[0]["AdWidth"].ToString() != "")
                {
                    rad.AdWidth = int.Parse(set.Tables[0].Rows[0]["AdWidth"].ToString());
                }
                if (set.Tables[0].Rows[0]["AdHight"].ToString() != "")
                {
                    rad.AdHight = int.Parse(set.Tables[0].Rows[0]["AdHight"].ToString());
                }
                if (set.Tables[0].Rows[0]["AdType"].ToString() != "")
                {
                    rad.AdType = int.Parse(set.Tables[0].Rows[0]["AdType"].ToString());
                }
                rad.AdText = set.Tables[0].Rows[0]["AdText"].ToString();
                rad.AdUrl = set.Tables[0].Rows[0]["AdUrl"].ToString();
                if (set.Tables[0].Rows[0]["StartDate"].ToString() != "")
                {
                    rad.StartDate = DateTime.Parse(set.Tables[0].Rows[0]["StartDate"].ToString());
                }
                if (set.Tables[0].Rows[0]["EndDate"].ToString() != "")
                {
                    rad.EndDate = DateTime.Parse(set.Tables[0].Rows[0]["EndDate"].ToString());
                }
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    rad.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    rad.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                rad.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                return rad;
            }
            return null;
        }

        public DataSet GetSpaceTime(int AdID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdID", SqlDbType.Int) };
            parameters[0].Value = AdID;
            return DbHelperSQL.RunProcedure("SellerAD_GetSpaceTime", parameters, "ds");
        }

        public int Update(KeRui.Model.SellerAD model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@SellerID", SqlDbType.Int, 4), new SqlParameter("@AdID", SqlDbType.Int, 4), new SqlParameter("@AdText", SqlDbType.NVarChar, 0xff), new SqlParameter("@AdUrl", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@StartDate", SqlDbType.DateTime), new SqlParameter("@EndDate", SqlDbType.DateTime), new SqlParameter("@AdName", SqlDbType.VarChar) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.SellerID;
            parameters[2].Value = model.AdID;
            parameters[3].Value = model.AdText;
            parameters[4].Value = model.AdUrl;
            parameters[5].Value = model.StartDate;
            parameters[6].Value = model.EndDate;
            parameters[7].Value = model.AdName;
            return DbHelperSQL.RunProcedure("SellerAD_Update", parameters, out num);
        }
    }
}

